package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //1. 根据name分页查询套餐列表
        //1-1 设置查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNoneEmpty(name), Setmeal::getName, name);

        //1-2 设置分页条件
        Page<Setmeal> page = new Page<>(pageNum,pageSize);

        //1-3 执行分页查询
        page = setmealMapper.selectPage(page, wrapper);
        System.out.println(page);

        //2. 遍历查询到的套餐列表, 设置根据每个套餐的分类名和菜品列表
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //2-1 查询到套餐分类名称
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
                //2-2 查询套餐下菜品信息
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishList);
            }
        }
        return page;
    }

    @Override
    public void save(Setmeal setmeal) {
        //1. 基本信息保存到套餐表, 返回套餐id
        setmealMapper.insert(setmeal);

        //2. 关联菜品信息,保存到中间表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                //设置套餐id
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public void deleteByIds(Long[] ids) {
        //1.确认删除的套擦处于在停售状态
        //select count(0) where id in #{ids} and status = 1   (在售)
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1);
        Integer num = setmealMapper.selectCount(wrapper);
        if (num > 0) {
            throw new CustomException("当前套餐有处于在售状态,不允许删除");
        }
        //2.执行删除
        for (Long id : ids) {
            //2.1删除中间表[根据套餐id]
            LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SetmealDish::getSetmealId, id);
            setmealDishMapper.delete(wrapper1);

            //2.2删除套餐[根据套餐id]
            setmealMapper.deleteById(id);
        }
    }

    @Transactional
    @Override
    public void updateByIds(Long[] ids, Integer status) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealMapper.updateById(setmeal);
        }
    }

    @Override
    public Setmeal findById(long id) {
        //1.查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //2.根据setmeal中的category_id 从category表查询name
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());

        // 3.根据setmeal的id从setmael_dish表中查询列表
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishList);
        return setmeal;
    }

    @Override
    public void update(Setmeal setmeal) {
        //1.根据主键更新菜品表信息
        setmealMapper.updateById(setmeal);

        //2.根据套餐id删除中间表中的相关菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(wrapper);

        //3.向中间表保存新的菜品列表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public List<Setmeal> findByCategoryId(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId).eq(Setmeal::getStatus, status);
        return setmealMapper.selectList(wrapper);
    }

    @Override
    public List<Dish> getmealDish(Long id) {
        //1. 从中间表获取菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);

        //2. 创建一个集合用于封装返回菜品信息
        List<Dish> dishList = new ArrayList<>();

        //3. 遍历得到菜品列表
        for (SetmealDish setmealDish : setmealDishList) {
            //根据菜品id从菜品表查询菜品详情
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            dish.setCopies(setmealDish.getCopies());//设置份数
            dishList.add(dish);//添加到返回集合
        }

        //4. 返回
        return dishList;
    }

}
